package com.aos.erp.gov.ex.web.rest;

import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.gov.ex.enums.GovConfigType;
import com.aos.erp.gov.ex.model.GovExConfig;
import com.aos.erp.gov.ex.model.GovExConfigItem;
import com.aos.erp.gov.ex.model.ro.GovExConfigDTO;
import com.aos.erp.gov.ex.model.to.GovExConfigItemTemplate;
import com.aos.erp.gov.ex.service.GovExConfigItemService;
import com.aos.erp.gov.ex.service.GovExConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController("RestGovExConfigController")
@RequestMapping("/api/gov/ex/config")
@Api("RestGovExConfigController")
public class GovExConfigController {

    @Resource
    private GovExConfigService govExConfigService;
    @Resource
    private GovExConfigItemService itemService;

    @GetMapping("/list")
    @ApiOperation(value = "分页查询,每个省市的配置方式")
    public ResultEntity<PageBean<GovExConfig>> queryPageBean(String provinceCode, String cityCode, @RequestParam(name = "pageIndex", required = false, defaultValue = "1") int pageIndex
            , @RequestParam(name = "pageSize", required = false, defaultValue = "20") int pageSize) {
        return ResultEntity.success(govExConfigService.queryByPage(provinceCode, cityCode, pageIndex, pageSize));
    }


    @GetMapping("/getDetailById")
    @ApiOperation(value = "获取详情，供界面展示，（包括没生效的配置项数据）")
    public ResultEntity<GovExConfigDTO> getDetailById(long id) {
        GovExConfig config = govExConfigService.getById(id);
        if (config != null) {
            GovExConfigDTO dto = new GovExConfigDTO();
            BeanUtils.copyProperties(config, dto);
            List<GovExConfigItem> itemList = itemService.getByConfig(config.getId(), config.getConfigType());
            dto.setConfigItemList(itemList);
            return ResultEntity.success(dto);
        }
        return ResultEntity.failed("不存在");
    }


    @PostMapping("/saveConfig")
    @ApiOperation(value = "配置保存，如果有id则为修改，如果id为空/0，则为新增,后台无校验")
    public ResultEntity saveConfig(@RequestBody GovExConfigDTO configDTO) {

        GovExConfig bean = new GovExConfig();
        BeanUtils.copyProperties(configDTO, bean);
        govExConfigService.saveConfig(bean, configDTO.getConfigItemList());

        return ResultEntity.success();
    }


    @GetMapping("/getConfigItemTemplateInfo")
    @ApiOperation(value = "配置项的模板信息")
    public ResultEntity<List<GovExConfigItemTemplate>> getConfigItemTemplateInfo() {
        List<GovExConfigItemTemplate> list = new ArrayList<GovExConfigItemTemplate>();
        list.add(new GovExConfigItemTemplate(GovConfigType.ftp, 1, "内网上传地址"));
        list.add(new GovExConfigItemTemplate(GovConfigType.ftp, 2, "ftp账号"));
        list.add(new GovExConfigItemTemplate(GovConfigType.ftp, 3, "ftp密码"));
        list.add(new GovExConfigItemTemplate(GovConfigType.api, 1, "内网API地址"));
        list.add(new GovExConfigItemTemplate(GovConfigType.manual, 1, "导出配置"));
        return ResultEntity.success(list);
    }
}